#!/bin/bash
# From bash man pages: "RANDOM Each time this parameter is referenced, 
#   a random integer between 0 and 32767 is generated.  The sequence of 
#   random numbers may be initialized by assigning a value to RANDOM.  
#   If RANDOM is unset, it loses its special properties, even if it is 
#   subsequently reset.
jobOutput=job-$RANDOM.log

# function intHandler is called when the script is given SIG-INT, typically from ^C
intHandler()
{
	echo
	echo "==== [CTRL]-C ===="
    echo "Do you want to kill your job, $jobID, if it is still running?"
    ans=""
    read -p "yes/[no]: " ans
	if [ "$ans" = "yes" ]; then
		hdjob.sh -kill $jobID
	else
		echo OK.  Your job will not be killed it will continue to run.
	fi
	echo "=================="
}

echo Capturing hadoop job client output here: $jobOutput
nohup time hdjar.sh $@ > $jobOutput 2>&1 &
clientPID=$!
echo Client PID=$clientPID

jobID=""
TEST_RUNNING=`ps -p ${clientPID} | grep ${clientPID}`
while [ "$jobID" = "" ] && [ -z "${TEST_RUNNING}" ]; do
	## There should be a breakout here in case the job exits due to error...
	echo "Looking for job id in client output..."
	sleep 5
	jobID=`grep "Running job:" $jobOutput| sed -e's/.*Running job: //'` 
	TEST_RUNNING=`ps -p ${clientPID} | grep ${clientPID}`
done;

if [ "$jobID" = "" ] && [ ! -z "${TEST_RUNNING}" ]; then
	echo JOB with PID $clientPID exited before a Hadoop JOB ID could be found.
	echo Check $jobOutput
	exit 1
fi

# using the trap command
# trap CTRL-C and execute the sorry() function:
trap intHandler INT

echo Hadoop Job ID: $jobID
echo Getting job conf from /hadoop/data/mapred/system/$jobID/job.xml to $jobID.xml:
#curl http://host:50030/jobconf.jsp?jobid=$jobID -o $jobID.html
hdfs.sh -get /hadoop/data/mapred/system/$jobID/job.xml ./$jobID.xml
ret=$!
if [ "$ret" != "0" ]; then echo "HDFS get failed to get job conf.."; fi
echo Job configuration has been downloaded to $jobID.xml

echo "Waiting for job to complete... tail -f $jobOutput: "

tail -f $jobOutput &
tailPID=$!

wait "$clientPID" 
ret=$?
echo $jobID has completed with return code $ret >> $jobOutput

#tail -10 $jobOutput
kill -INT $tailPID

echo Moving job output from $jobOutput to job-$jobID.log
mv $jobOutput job-$jobID.log
echo done.

